/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.controller;

import com.tesam.gedeon.entity.SystemUser;
import com.tesam.gedeon.service.SystemUserService;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 

import java.util.*;
import java.util.concurrent.ExecutionException;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Sistema
 */
public class securityFilter  implements Filter  {
    
    @Autowired
    
//    @Resource(name="SystemUserDaoImpl")
//    private SystemUserService User   ;
//    private String sessionId ;
//    SystemUser user = new SystemUser();
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
      
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException , ServletException {
          
           HttpServletResponse resp = (HttpServletResponse)response;
           
           boolean login = false;
           boolean cookieState = false ;
           
           HttpSession newSession  ; 
            
           HttpServletRequest reques ;
                        
           reques = (HttpServletRequest) request ;
           
           String url = reques.getRequestURL().toString();
           try{
                     
           newSession = reques.getSession(false) ;
            
           if(!url.contains("/login/login") && !url.contains("/login/index")){
               
            try{
               if(newSession != null){
                 if(newSession.getAttribute("sessionId") != null){
                    if(!newSession.getAttribute("sessionId").equals("")){ 
                    login = false ;
                    }else{
                    login = true ;
                    }
                  }else{
                 login = true ;
                 }
            }else{
            login = true ;
            }
           }catch(Exception e){
              login = true ;  
              System.out.println(e.getMessage());
            }
              
           } 
            
            Cookie cookie = null;
            Cookie[] cookies = null;
            // Get an array of Cookies associated with this domain
            cookies = reques.getCookies(); 
            if( cookies != null ){
            System.out.println("<h2> Found Cookies Name and Value</h2>"+cookies.length);
                for (int i = 0; i < cookies.length; i++){
                cookie = cookies[i];
                    if(cookie.getName().equals("billing") && !cookie.getValue().equals("")){
                    login = false ;
                    System.out.println("Valor de Cookie : "+cookie.getValue().toString());
                    cookieState = true ;
                    }
                }
            }else{
                login = true ;
            }
           
           }catch(Exception e){
           login = true ; 
           System.out.println(e.getMessage());
           }
            
           try{
             
           if(login){ 
              resp.sendRedirect("/GedeonMvc/login/logout");
           }
           
           chain.doFilter(request, response);
           }catch(Exception e){
                System.out.println(e.getMessage());
           }
           
    }

    @Override
    public void destroy() { 
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
  
    
}
